#define _CRT_SECURE_NO_DEPRECATE
#include "grate_config.h"
#include <cstdio>
#include <cstring>

grate_config::grate_config(){
  auto_queue_size = true;
  auto_dfa_size = true;

  MAXITER = 20000; // Iterations the algorithm with oracle does

  USE_ORACLE = false; // true - Algo1Q, false - Algo1
  ADD_LOOPS = false; // if we add loops, it makes algorithm work slower, but DFA is always primitive
  IMPROVE_TARJAN = false; // if true, assumes that the resulting dfa always have one scc, this saves memory
  GEN_EDGE_LIST = -1; // replaces DFA with edge list, sometimes this works faster (0 - don't use, 1 - use, -1 - auto)

  OUTPUT_PROGRESS = true; // writes additional data to the log file
}

void grate_config::parse_config_file(){
  FILE * inif = fopen("params.ini", "r");
  char str[100], val[100];
  while (fscanf(inif, "%s = %s\n", &str, &val) > 0){
    if (!strcmp(str, "QUEUE_SIZE")){
      if (sscanf(val, "%d", &QUEUE_SIZE) > 0){
        auto_queue_size = false;
      }
    }
    if (!strcmp(str, "DFA_SIZE")){
      if (sscanf(val, "%d", &DFA_SIZE) > 0){
        auto_dfa_size = false;
      }
    }
    if (!strcmp(str, "MAX_ITER")){
      sscanf(val, "%d", &MAXITER);
    }
    if (!strcmp(str, "OUTPUT_PROGRESS")){
      OUTPUT_PROGRESS = (val[0]=='t' || val[0]=='T' || val[0]=='1');
    }
    if (!strcmp(str, "USE_ORACLE")){
      USE_ORACLE = (val[0]=='t' || val[0]=='T' || val[0]=='1');
    }
    if (!strcmp(str, "ADD_LOOPS")){
      ADD_LOOPS = (val[0]=='t' || val[0]=='T' || val[0]=='1');
    }
    if (!strcmp(str, "GEN_EDGE_LIST")){
      if (val[0]=='t' || val[0]=='T' || val[0]=='1') GEN_EDGE_LIST = 1;
      else if (val[0]=='f' || val[0]=='F' || val[0]=='0') GEN_EDGE_LIST = 0;
      else GEN_EDGE_LIST = -1;
    }
    if (!strcmp(str, "IMPROVE_TARJAN")){
      IMPROVE_TARJAN = (val[0]=='t' || val[0]=='T' || val[0]=='1');
    }
  }
}
